package com.qch.concurrent.demo_02_countdownLatch;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * @Author qch
 * @Date 2021/8/2
 * 假设一个主线程要等待5个 Worker 线程执行完才能退出
 */
public class MyThread extends Thread {
    private final CountDownLatch latch;
    private final Random random = new Random();

    public MyThread(String name,CountDownLatch latch) {
        super(name);
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(random.nextInt(2000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"-执行完毕");
        //latch计数减一
        latch.countDown();
    }
}
